package com.huawei.studentms.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

/**
 * @author 黄后林
 * @date 2022-10-29 16:07:39
 * @note 程序员是用代码喂出来的
 */
public class JDBCUtil {

    private static final String DRIVER="com.mysql.jdbc.Driver";
    private static final String URL="jdbc:mysql://127.0.0.1:3306/studentdb";
    private static final String USER="root";
    private static final String PASSWORD="root";


    /**
     * 方法描述:  获取数据库连接
     * @author: 黄后林
     * @date: 2022/10/29 16:37
     * @param
     * @return java.sql.Connection
     */
    public static Connection getConnection() throws  Exception{
        //1、加载驱动  com.mysql.jdbc.Driver
        Class.forName(DRIVER);
        //2、获取连接
        Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
        return conn;
    }

    public static void closeConnection(PreparedStatement ps,Connection conn) throws Exception {

        if(ps != null){
            ps.close();
        }

        if(conn != null){
            conn.close();
        }
    }

}
